package vn.sms.vsms;

import java.util.ArrayList;

 







import vn.sms.vsms.R;
import vn.sms.vsms.data.ServiceItem;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class TopMenuAdapter extends BaseAdapter {
	private Context context;
	private  ArrayList<ServiceItem> services;
	private int height;
	private int width;
	public TopMenuAdapter(Context context, ArrayList<ServiceItem> services,int height,int width) {
		super();
		this.context = context;
		this.services = services;
		this.height = height;
		this.width  = width;
	}

	@Override
	public int getCount() {
		return this.services.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View item = arg1;
		if(item == null)
		{
			item = new View(context);
			 
			// get layout from mobile.xml
			int h = context.getResources().getDisplayMetrics().densityDpi;

			item = inflater.inflate(R.layout.top_service_item, null);
			int numColums = 3;
			int padding = 20;
			int numRows = services.size()/numColums;
			int itemheight = (height - (numRows + 2)*padding)/numRows;
			int itemWidth  = (width - (numColums + 1)*padding) / numColums;
			

			
	        //holder.image.setLayoutParams(new LayoutParams(h-50,h-50));
	        item.setLayoutParams(new GridView.LayoutParams(itemWidth, itemheight));
			// set value into textview
			
	
		}		
		
		final ServiceItem service = this.services.get(arg0);
		TextView textView = (TextView) item
				.findViewById(R.id.item_txt);		
		
		// set image based on selected text
		ImageView imageView = (ImageView) item
				.findViewById(R.id.item_img);
		if(service != null){
			int drawableResourceId = context.getResources().getIdentifier(service.getIcon(), "drawable", context.getPackageName());
			imageView.setImageResource(drawableResourceId);
			textView.setText(service.getName());
			
			item.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Contants.onItemClick(v.getContext(), service);
					
				}
			});
		}
		
		
		
		return item;
	}

}
